package com.liu.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

/**
 * @Author: 刘浩然
 * @Date: 2020/3/24 18:08
 */
@Controller
public class UserController {
    @PostMapping("/doLogin")
   public String doLogin(String username, String  password, Model model){
       UsernamePasswordToken token =new UsernamePasswordToken(username,password);
       Subject subject = SecurityUtils.getSubject();//获取一个Subject对象
       try{
           subject.login(token);
       }catch (AuthenticationException e){
           model.addAttribute("error","用户名或密码输入错误!");
           return "login";
       }
      return  "redirect:/index";
   }
   @RequiresRoles("admin")
   @GetMapping("/admin")
   public String admin(){
        return "admin";
   }

   @RequiresRoles(value = {"admin","user"},logical= Logical.OR)
   @GetMapping("/user")
   public String user(){
        return "user";
   }

}
